from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa

# 生成密钥对
private_key = rsa.generate_private_key(
    public_exponent=65537,
    key_size=2048,
    backend=default_backend()
)

# 从私钥中获取公钥
public_key = private_key.public_key()

# 将公钥转换为PEM格式
pem_public_key = public_key.public_bytes(
    encoding=serialization.Encoding.PEM,
    format=serialization.PublicFormat.SubjectPublicKeyInfo
)

# 将PEM格式的公钥写入文件
with open('../filesOutput/public_key.pem', 'wb') as pem_file:
    pem_file.write(pem_public_key)